package com.example.service;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.AirLane;

import com.example.entity.BaseAirLane;
import com.example.trans.transBaseAirLane;
import com.example.mapper.BaseAirLaneMapper;
import com.example.service.interfaces.BaseAirLaneInterface;
import com.example.trans.AirLaneWp;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @program: microservice
 * @description:
 * @author: Aprilies
 * @create: 2024-09-10 16:51
 **/

@Service
public class BaseAirLaneService extends ServiceImpl<BaseAirLaneMapper, BaseAirLane> implements BaseAirLaneInterface {

    @Autowired
    private BaseAirLaneMapper baseAirLaneMapper ;

    public List<transBaseAirLane> findAll(){
        List<BaseAirLane> baseAirLanes = baseAirLaneMapper.selectList(null);
        System.out.println(baseAirLanes);
        return baseAirLanes.stream().map(baseAirLane -> {
            transBaseAirLane transBaseAirLane = new transBaseAirLane();

            BeanUtils.copyProperties(baseAirLane, transBaseAirLane);
            String wp = baseAirLane.getWaypointsCoordinates();
            System.out.println(wp);
            if(wp != null && !wp.isEmpty()) {
                JSONArray jsonArray = JSONArray.parseArray(wp);
                List<AirLaneWp> airLaneWps = jsonArray.toJavaList(AirLaneWp.class);
                System.out.println(airLaneWps);
                transBaseAirLane.setWaypointsCoordinates(airLaneWps);
            }
            return transBaseAirLane;
        }).collect(Collectors.toList());
    }

    public Optional<transBaseAirLane> findByID(Integer id) {
        BaseAirLane baseAirLane = baseAirLaneMapper.selectById(id);
        if (baseAirLane == null) {
            return Optional.empty();
        }
        transBaseAirLane transBaseAirLane = new transBaseAirLane();
        BeanUtils.copyProperties(baseAirLane, transBaseAirLane);
        // 单独处理 waypointsCoordinates 字段
        String wp = baseAirLane.getWaypointsCoordinates();
        if (wp != null && !wp.isEmpty()) {
            JSONArray jsonArray = JSONArray.parseArray(wp);
            List<AirLaneWp> airLaneWps = jsonArray.toJavaList(AirLaneWp.class);
            transBaseAirLane.setWaypointsCoordinates(airLaneWps);
        }

        return Optional.of(transBaseAirLane);
    }


    public transBaseAirLane isave(transBaseAirLane transLane) {
        BaseAirLane baseAirLane = new BaseAirLane();
        // 使用 BeanUtils 复制属性
        BeanUtils.copyProperties(transLane, baseAirLane);

        // 将 List<AirLaneWp> 转换为 JSON 字符串并设置到 waypointsCoordinates
        List<AirLaneWp> waypoints = transLane.getWaypointsCoordinates();
        if (waypoints != null && !waypoints.isEmpty()) {
            String wpJson = JSONArray.toJSONString(waypoints);
            baseAirLane.setWaypointsCoordinates(wpJson);
        }
        // 插入数据
        baseAirLaneMapper.insert(baseAirLane);
        transLane.setId(baseAirLane.getId());
        return transLane;
    }


    // 只返回更新的字段数据
    public Optional<transBaseAirLane> update(transBaseAirLane user) {
        BaseAirLane baseAirLane = new BaseAirLane();
        BeanUtils.copyProperties(user, baseAirLane);
        List<AirLaneWp> waypoints = user.getWaypointsCoordinates();
        if (waypoints != null && !waypoints.isEmpty()) {
            String wpJson = JSONArray.toJSONString(waypoints);
            baseAirLane.setWaypointsCoordinates(wpJson);
        }
        int i = baseAirLaneMapper.updateById(baseAirLane);
        if(i == 1){
            return Optional.of(user);
        }
        return Optional.empty();
    }

    public boolean delete(Long id) {
        if (baseAirLaneMapper.selectById(id) != null) {
            baseAirLaneMapper.deleteById(id);
            return true;
        }
        return false;
    }

}
